Scout.inherits( Paintable );
function Scout(x, y) {
	"use strict";
	
	this.inherits(Paintable, PaintableTypes.SCOUT, x, y);

	this.angle = 0;

	// Current target
	this.path = null;	
	this.nextTaget = null;	
}

//Speed in tiles per second
Scout.LINEAR_SPEED=3; 

//Speed in radians per second
Scout.ANGULAR_SPEED=4; 

// update model for rendering
Scout.prototype.update = function() {
	//
	if (this.nextTaget) {
		//
		//	Rotates
		var targetAlpha = Math.atan2(this.nextTaget[1]-this.y,this.nextTaget[0]-this.x);
		if (targetAlpha < 0) {targetAlpha += 2 * Math.PI;}
		//	
		var angleDistance = targetAlpha-this.angle;
		var direction=1;
		if(angleDistance>Math.PI){angleDistance-=Math.PI;direction=-1;}
		if(angleDistance<-Math.PI){angleDistance+=Math.PI;direction=-1;}
		//	
		var maxAngleOffset = Scout.ANGULAR_SPEED * Game.model.deltaTimestamp/1000;
		if (angleDistance>maxAngleOffset) {
			angleDistance=maxAngleOffset;
		}
		if (angleDistance<-maxAngleOffset) {
			angleDistance=-maxAngleOffset;
		}
		//	
		this.angle+=direction*angleDistance;
		//
		while (this.angle<0){
			this.angle+=2*Math.PI;
		}
		while (this.angle>2*Math.PI){
			this.angle-=2*Math.PI;
		}
		//
		//	Moves
		var maxOffset = Scout.LINEAR_SPEED * Game.model.deltaTimestamp/1000;
		var distance = CoordinatesUtil.computePointDistance(this.x, this.y, this.nextTaget[0], this.nextTaget[1]);
		if (distance > maxOffset) {
			distance=maxOffset;
		}
		//
		if ( (targetAlpha-this.angle)>-(Math.PI/16) && (targetAlpha-this.angle)<(Math.PI/16) ) {
			var dx = this.nextTaget[0]-this.x;
			var dy = this.nextTaget[1]-this.y;
			var newX= this.x +distance * Math.cos(this.angle) ;
			var newY= this.y +distance * Math.sin(this.angle) ;
			//
			Game.model.objectBag.updateNodeCoordinates(this, {x: newX, y: newY});
		}
		//
		if (distance < maxOffset) {
			if (this.path.length>0){
				// Store next step 
				this.nextTaget = this.path[0];
				// Remove next step from path
				this.path.splice(0,1);
			}else{
				this.nextTaget=null;
				this.path=null;
			}
		}
	}
};

Scout.getFrameNumberForAngle= function(angle){
	// * 32 / 2 * Math.PI ====> * 16 / Math.PI 
	var no = Math.floor( angle * 16 /  Math.PI % 32) + 1;
	if (no > 32) {
		no = 32;
	}
	if (no < 1) {
		no = 1;
	}
	return no;
};

// Renders object
Scout.prototype.render = function(ctx) {
	var time, no, imgId, x, y, xx, yy;
	time = Game.model.currentTimestamp * 0.002;
	//
	imgId = "scout_"+Scout.getFrameNumberForAngle(this.angle);
	//
	if (this.isSelected()) {
		RenderUtil.render(ctx, 'selector1x1', this.x, this.y, 0.5, 1);
	}
	//
	RenderUtil.render(ctx, imgId, this.x, this.y, this.paintableType.offsetX, this.paintableType.offsetY);
	//
	PFUtil.drawPath(ctx, this.x, this.y, this.nextTaget, this.path);
};

// goTo command
Scout.prototype.goTo = function(x, y) {
	//
	this.path=PFUtil.findPath(this.x, this.y, x, y);
	//
	// If valid coordinates and path found
	if (this.path){
		// Store next step 
		this.nextTaget = this.path[0];
		// Remove next step from path
		this.path.splice(0,1);
	}else{
		// Clean next step 
		this.nextTaget = null;
	}
};
